package cn.oscar.service.imp;

import cn.hutool.core.bean.BeanUtil;
import cn.oscar.mapper.BlogMapper;
import cn.oscar.service.BlogService;
import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.sun.xml.internal.bind.v2.TODO;
import commonUtils.clients.CommentClient;
import commonUtils.dto.AdminDTO;
import commonUtils.dto.BlogDTO;
import commonUtils.entities.Administrator;
import commonUtils.entities.Blog;

import commonUtils.entities.Comment;
import commonUtils.entities.Restful;
import commonUtils.utils.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@Service
public class BlogServiceImp extends ServiceImpl<BlogMapper, Blog> implements BlogService {

    @Autowired
    private BlogMapper blogMapper;

    @Autowired
    private CommentClient commentClient;


    @Override
    public Restful<Page<BlogDTO>> selectByPage(int page, int pageSize, String name) {
        Page<Blog> pageInfo = new Page<>(page,pageSize);
        Page<BlogDTO> pageInfoDto = new Page<>();

        LambdaQueryWrapper<Blog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name),Blog::getTitle,name);
        queryWrapper.orderByDesc(Blog::getCreateTime);
//        queryWrapper.eq(Blog::getIsLock,1);
        page(pageInfo,queryWrapper);

        //分页查询数据复制给dto
        BeanUtil.copyProperties(pageInfo,pageInfoDto,"records");

        List<Blog> records = pageInfo.getRecords();
        List<BlogDTO> recordsDto = records.stream().map((blog)->{
            BlogDTO blogDTO = new BlogDTO();
            BeanUtil.copyProperties(blog,blogDTO);
            return blogDTO;
        }).collect(Collectors.toList());

        pageInfoDto.setRecords(recordsDto);

        return Restful.success(pageInfoDto);
    }

    @Override
    public Restful<String> deleteByIds(Long[] ids) {
        if (ids.length > 0){
            for (Long id : ids) {
                removeById(id);
            }
        }else{
            return Restful.error("数据非法");
        }
        return Restful.success("博客删除成功");

    }

    @Override
    public Restful<String> lockBlog(Blog blog) {
        int i = blog.getIsLock() == 0 ? 1 : 0 ;
        blog.setIsLock(i);
        return Restful.success("博客锁定状态修改成功");
    }

    @Override
    public Restful<String> addBlog(Blog blog) {
        Long userId = BaseContext.getCurrentId();
        blog.setUserId(userId);
        blog.setCreateTime(LocalDateTime.now());
        save(blog);
        return Restful.success("新增博客成功");
    }

    @Override
    public Restful<BlogDTO> getBlogById(Long id) {
        Blog blog = getById(id);
        // TODO: 2022/11/24   将评论信息和博客信息绑定
        List<Comment> comment = commentClient.selectCommentByBlogId(id).getData();
        log.info("博客的内容是：" + blog);
        log.info("该博客对于的评论是：" + comment);
        BlogDTO blogDTO = new BlogDTO();
        blogDTO.setBlog(blog);
        blogDTO.setCommentList(comment);
        System.out.println(blogDTO);
        return Restful.success(blogDTO);
    }
}
